import javax.servlet.ServletContext;

public class S13_WebUtils {

    public static int visitCount(ServletContext servletContext) {
        //从servletContext获取 visit_count 属性 k-v
        Object visitCount = servletContext.getAttribute("visit_count");
        //判断visit_count是否为空
        if (visitCount == null) {   //说明是第一次访问
            servletContext.setAttribute("visit_count", 1);
            visitCount = 1;
        } else {    //第二次或以后
            //取出visit_count的值，再+1
            visitCount = Integer.parseInt(visitCount.toString()) + 1;
            //放回 servletContext
            servletContext.setAttribute("visit_count", visitCount);
        }
        return Integer.parseInt(visitCount.toString());
    }
}
